package com.kede.api.stand.service.impl;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.NullSerializer;
import com.kede.api.stand.service.GoodService;
import com.kede.config.NullValueSerializer;
import com.kede.dictenum.BoolDictEnum;
import com.kede.domain.LeescoreGoodTypeIdsDomain;
import com.kede.domain.LeescoreGoodsTypeDomain;
import com.kede.ienum.FunStatusEnum;
import com.kede.pojo.ResHttp;
import com.kede.service.LeescoreGoodTypeIdsService;
import com.kede.service.LeescoreGoodsTypeService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/***
 ** @Author:Pr.li
 ** @Description:
 ** @Date: create in 2023/5/27 16:49
 ***/
@Service
@RequiredArgsConstructor
public class GoodServiceImpl implements GoodService {

    private final LeescoreGoodsTypeService goodsTypeService;

    private final LeescoreGoodTypeIdsService goodTypeIdsService;

    private static Integer ZENO = 0;


    @Override
    public ResHttp getGoodTypeDetail(Integer gid) {
        LambdaUpdateWrapper<LeescoreGoodTypeIdsDomain> querywrapper  = new LambdaUpdateWrapper<>();
        querywrapper.eq(LeescoreGoodTypeIdsDomain::getPid,ZENO);
        querywrapper.eq(LeescoreGoodTypeIdsDomain::getGoodId,gid);
        //一级选品
        List<LeescoreGoodTypeIdsDomain> list = goodTypeIdsService.list(querywrapper);
        List<GoodTypeVo> result = new ArrayList<>();
        Iterator<LeescoreGoodTypeIdsDomain> iterator = list.iterator();
        while (iterator.hasNext()){
            LeescoreGoodTypeIdsDomain domain = iterator.next();
        }
        list.forEach(domain->{
            LeescoreGoodsTypeDomain goodtype = goodsTypeService.getById(domain.getGooTypeId());
            if (goodtype.getStatus().equals(FunStatusEnum.ON.getValue())){
                GoodTypeVo pgoodTypeVo = new GoodTypeVo();
                pgoodTypeVo.setName(goodtype.getName());
                pgoodTypeVo.setId(goodtype.getId());
                List<GoodTypeVo> childlist = new ArrayList<>();
                //子选品
                LambdaUpdateWrapper<LeescoreGoodTypeIdsDomain> childquerywrapper  = new LambdaUpdateWrapper<>();
                childquerywrapper.eq(LeescoreGoodTypeIdsDomain::getPid,domain.getId());
                childquerywrapper.eq(LeescoreGoodTypeIdsDomain::getGoodId,gid);
                List<LeescoreGoodTypeIdsDomain> secList = goodTypeIdsService.list(childquerywrapper);
                secList.forEach(child->{
                    LeescoreGoodsTypeDomain ctype = goodsTypeService.getById(child.getGooTypeId());
                    if (ctype.getStatus().equals(FunStatusEnum.ON.getValue())){
                        GoodTypeVo cgoodTypeVo = new GoodTypeVo();
                        cgoodTypeVo.setId(ctype.getId());
                        cgoodTypeVo.setName(ctype.getName());
                        childlist.add(cgoodTypeVo);
                    }
                });
                pgoodTypeVo.setChildren(childlist);
                if (childlist.size() > 0){
                    result.add(pgoodTypeVo);
                }
            }
        });
        return ResHttp.okData(result);
    }

    @Data
    static class GoodTypeVo{
        Integer id;
        String name;

        @JsonSerialize(nullsUsing = NullValueSerializer.class)
        List<GoodTypeVo> children = null;
    }
}
